/*
 * IcmpSessionManager.cpp
 *
 *  Created on: 15/08/2011
 *      Author: george
 */

#include "icmpsessionmanager.h"
#include <iostream>
bool IcmpSessionManager::add(struct in_addr *src, struct in_addr *dst, icmp_hdr_t *p){
	IcmpSession* icmpsession = getSession(src, dst, &p->un.echo.id);
	icmpsession->add(p);
	return true;
}


IcmpSession* IcmpSessionManager::getSession(in_addr *src,in_addr *dst, u_int16_t *id){
	unsigned pos = 0;

	while (pos < session.size() ){

		bool mesmo_src = (session[pos]->src.s_addr == src->s_addr);
		bool mesmo_dst = (session[pos]->dst.s_addr == dst->s_addr);
		bool mesmo_id =  (session[pos]->id == *id);

		if ( mesmo_dst && mesmo_src && mesmo_id )
		{
			return session[pos];
		}

		mesmo_src = (session[pos]->src.s_addr == dst->s_addr);
		mesmo_dst = (session[pos]->dst.s_addr == src->s_addr);
		mesmo_id  =  (session[pos]->id == *id);

		if ( mesmo_dst && mesmo_src && mesmo_id )
		{
			return session[pos];
		}

		++pos;
	}

	session.push_back(new IcmpSession(*src, *dst, *id));

	return session[pos];
}


IcmpSessionManager::IcmpSessionManager() {
}


IcmpSessionManager::~IcmpSessionManager() {
}
